package com.azzcs.coyote.connector;

import com.azzcs.Catalina;
import com.azzcs.coyote.Adapter;
import com.azzcs.coyote.CoyoteAdapter;
import com.azzcs.coyote.Http11NioProtocol;
import com.azzcs.coyote.ProtocolHandler;
import com.azzcs.lifecycle.LifecycleBase;
import com.azzcs.service.Service;

import java.util.concurrent.Executor;

/**
 * @Author: wzg
 * @Date: 2021/1/7 下午4:37
 */
public class Connector extends LifecycleBase {

    protected final ProtocolHandler protocolHandler = new Http11NioProtocol();
    protected Adapter adapter = new CoyoteAdapter(this);
    protected Service service;
    private String executorName;
    protected int port = -1;
    private Catalina catalina;

    public Connector(int port, Service service, String executorName, Catalina catalina) {
        this.port = port;
        this.service = service;
        this.executorName = executorName;
        this.catalina = catalina;
    }

    @Override
    protected void initInternal() {
        Executor executor = service.getExecutor(executorName);
        protocolHandler.setExecutor(executor);
        protocolHandler.setAdapter(adapter);
        protocolHandler.serMapper(catalina.getMapper());
        protocolHandler.init();
    }

    @Override
    protected void startInternal() {
        protocolHandler.start();
    }

}
